﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using SdmxMl.Manager;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Excel;

namespace SdmxMl.Fact_Dim
{
    class CollectHierarchyInfo
    {
        protected Dictionary<string, CodeListType> dico;
        private HierarchicalCodelistType hcl;
        private HierarchyType hierarchy;
        private RefNomen refVarHelper;
        private AnnotationInventory anInventory;
        private int finalLevel;

        #region Constructor

        public CollectHierarchyInfo(RefNomen refMeta, HierarchicalCodelistType h, int final)
        {
            refVarHelper = refMeta;
            hcl = h;
            finalLevel = final;
            hierarchy = h.HierarchyList[0];
            dico = GetRefDictionary();


            anInventory = new AnnotationInventory();
            foreach (KeyValuePair<string, CodeListType> kvp in dico)
                foreach (CodeType c in kvp.Value.CodeList)
                    anInventory.Inventory(c.AnnotationList, true);

        }

        #endregion

        private Dictionary<string, CodeListType> GetRefDictionary()
        {
            Dictionary<string, CodeListType> dico = new Dictionary<string, CodeListType>();
            foreach (CodelistRefType crt in hierarchy.CodelistRefList)
            {
                CodeListType clType = null;
                Artefact a = refVarHelper.GetCodelist(crt);
                if (a != null)
                    clType = a as CodeListType;
                if (clType != null)
                    dico.Add(crt.Alias, clType);
            }
            return dico;
        }

        public string CreateOracleSql()
        {
            StringBuilder sb = new StringBuilder(1024);
            OraFactDim oraSql = new OraFactDim(refVarHelper);

            sb.Append(oraSql.CreateHierarchyDrop(hcl));

            TreeNode rootNode = new TreeNode();
            hierarchy.CodeRefList.GetTreeHierarchyAsNodes(rootNode, dico);

            sb.Append(oraSql.CreateDimHierarchyCode(dico, rootNode, hcl, finalLevel));

            return sb.ToString();
        }


    }

    class CollectCodelistInfo
    {
        private CodeListType clt;
        private RefNomen refVarHelper;

        #region Constructor

        public CollectCodelistInfo(RefNomen refMeta, CodeListType c)
        {
            refVarHelper = refMeta;
            clt = c;
        }

        #endregion


        public string CreateOracleSql()
        {
            StringBuilder sb = new StringBuilder(1024);
            OraFactDim oraSql = new OraFactDim(refVarHelper);

            sb.Append(oraSql.CreateCodelistDrop(clt));

            sb.Append(oraSql.CreateDimCode(clt, null));

            return sb.ToString();
        }


    }
}
